package com.ddyx.common.utils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: DateUtil.java
 * @Description: 时间工具
 * @author: liuhoujie
 * @date: 2018年3月27日
 */
public class DateUtil {

	public static SimpleDateFormat format_ymd_hms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static SimpleDateFormat format_ymd = new SimpleDateFormat("yyyy-MM-dd");

	public static String getNow() {
		return format_ymd_hms.format(new Date());
	}

	public static String getToday_Ymd() {
		return format_ymd.format(new Date());
	}

	public static String getFirstDayOfThisWeek_Ymd() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int d = 0;
		if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
			d = -6;
		} else {
			d = 2 - cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		return format_ymd.format(cal.getTime());
	}

	public static String getLastDayOfThisWeek_Ymd() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int d = 0;
		if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
			d = -6;
		} else {
			d = 2 - cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		cal.add(Calendar.DAY_OF_WEEK, 6);
		return format_ymd.format(cal.getTime());
	}

	public static String getUUID() {
		return UUID.randomUUID().toString();
	}


	/**
	 * @DESC 时间戳转时间date格式 
	 * @param objectDate
	 * @return
	 */
	public static Date toDate(Object objectDate) {
		try {
			objectDate = Long.valueOf(objectDate+"");
			String strDate = format_ymd_hms.format(objectDate);
			Date date = format_ymd_hms.parse(strDate);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date toDate(Long longDate) {
		try {
			String strDate = format_ymd_hms.format(longDate);
			Date date = format_ymd_hms.parse(strDate);
			return date;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date addHours(Date date,int hours) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.HOUR, hours);
			return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date addDays(Date date,int days) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DAY_OF_MONTH, days);
			return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date addMonths(Date date,int months) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, months);
			return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String printStr(Object objectDate) {
		try {
			String strDate = format_ymd_hms.format(objectDate);
			return strDate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String printStrDate(Object objectDate) {
		try {
			String strDate = format_ymd.format(objectDate);
			return strDate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	// 获得某天最大秒级时间 2017-10-15 23:59:59
	public static Date getEndOfDay(Date date) {

		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime endOfDay = localDateTime.with(LocalTime.of(23,59,59));
		return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}

	// 获得某天最小秒级时间 2017-10-15 00:00:00
	public static Date getStartOfDay(Date date) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
		LocalDateTime startOfDay = localDateTime.with(LocalTime.of(0,0,0));
		return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
	}

	public static Date getStartOfPlusDay(Date date,Integer plus) {
		Instant instant = date.toInstant();
		ZoneId zoneId = ZoneId.systemDefault();
		LocalDateTime localDate = instant.atZone(zoneId).toLocalDateTime();
		LocalDateTime plusTime = localDate.plus(plus, ChronoUnit.DAYS);
		ZonedDateTime zdt = plusTime.atZone(zoneId);
		Date from = Date.from(zdt.toInstant());
		return DateUtil.getStartOfDay(from);
	}

	public static Date getEndOfPlusDay(Date date,Integer plus) {
		Instant instant = date.toInstant();
		ZoneId zoneId = ZoneId.systemDefault();
		LocalDateTime localDate = instant.atZone(zoneId).toLocalDateTime();
		LocalDateTime plusTime = localDate.plus(plus, ChronoUnit.DAYS);
		ZonedDateTime zdt = plusTime.atZone(zoneId);
		Date from = Date.from(zdt.toInstant());
		return DateUtil.getEndOfDay(from);
	}
	
	/**
	 * 当前开始时间
	 * @return
	 */
	public static Date getStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}
 
	
	/**
	 * 当前结束时间
	 * @return
	 */
	public static Date getEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 判断日期时间是否在7天之内
	 * @param addtime 是要校核的时间
	 * @param now 是获取的当前是时间
	 * @return
	 * add by qiyifei
	 */
	public static boolean isLatestWeek(Date addtime, Date now){
		Calendar calendar = Calendar.getInstance();  //得到日历
		calendar.setTime(now);//把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为7天前
		Date before7days = calendar.getTime();   //得到7天前的时间
		if(before7days.getTime() < addtime.getTime()){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @description 时间戳(10位)转date
	 * @author NullPoint
	 * @date 2019年4月19日
	 * @param timestamp(Long)
	 * @return
	 */
	public static Date timestampToDate(Long timestamp) {
		return null;
	}
	/**
	 * @description 时间戳(10位)转date
	 * @author NullPoint
	 * @date 2019年4月19日
	 * @param timestamp(Object)
	 * @return
	 */
	public static Date timestampToDate(Object timestamp) {
		Date date=null;
		try {
			if (timestamp!=null) {
				Long ts = Long.valueOf(timestamp + "") * 1000;
				date = format_ymd_hms.parse(format_ymd_hms.format(ts));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}
